﻿using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Threading;

namespace CrawlerConsole
{
    class Program
    {
        static void Main(string[] args)
        {
             
            BaseProject();

            Console.WriteLine("Crawler Main!");
            
            Console.ReadKey();
        }


        private static void setParam()
        {
            ChromeOptions options = new ChromeOptions();
            //Set the http proxy value, host and port.
            //Set the proxy to the Chrome options
            var proxy = new Proxy();
            proxy.Kind = ProxyKind.Manual;
            proxy.IsAutoDetect = false;
            proxy.HttpProxy = "localhost:8080";
            proxy.SslProxy = "localhost:8080";
            options.Proxy = proxy;

            options.AddArgument("ignore-certificate-errors");
            //options.AddArgument("--headless");
            options.AddArgument("--disable-gpu");

            string url = @"https://search.yahoo.co.jp/";
            var spua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25";
            var pcua = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36";

            options.AddArgument(string.Format("--user-agent={0}", pcua));
            List<String> tagNmaeList = new List<string>(); ;
            var driver = new ChromeDriver(options);
            string Source = null;
            try
            {


                //002的换用另外程序
                driver.Navigate().GoToUrl(url);
                var inputNode = driver.FindElementByXPath(@"//input[@name='p']");
                Thread.Sleep(3000);
                inputNode.SendKeys("手机");
                var search = driver.FindElementByXPath(@"//input[@type='submit']");
                Thread.Sleep(2000);
                search.Click();
                Thread.Sleep(5000);
                Source = driver.PageSource;


            }
            catch (Exception ex)
            {


            }
            finally
            {
                // driver.Close();

                driver.Quit();


            }
            System.IO.File.WriteAllText("", Source, System.Text.Encoding.UTF8);

            Console.WriteLine(Source);
        }

        private static void BaseProject()
        {
            //获取运行目录路径
            var directory = System.IO.Directory.GetCurrentDirectory();
            //得到chromedriver.exe目录路径
            string path = directory + "/../../../tool";

            //隐藏默认ChromeDriver是会提示“Chrom正受到自动测试软件控制”的
            ChromeOptions options = new ChromeOptions();
            options.AddExcludedArgument("enable-automation");
            options.AddAdditionalCapability("useAutomationExtension", false);

            //是否隐藏浏览器，如果注释掉的话，那么会在启动的时候打开谷歌浏览器，在页面上可以看到填单的操作，如果取消的话，就只有cmd窗口，谷歌浏览器隐匿运行        
            //options.AddArgument("headless");
            //options.AddArgument("window-size=1920,1080");
             
            ChromeDriverService service = ChromeDriverService.CreateDefaultService(path);
            //设置cmd窗口不弹出来,控制台下不输入执行信息
            service.HideCommandPromptWindow = true;

            //初始化IWebDriver对象 
            IWebDriver driver = new ChromeDriver(service, options);
              
            //访问url页面
            driver.Navigate().GoToUrl("https://www.baidu.com");

            driver.FindElement(By.Name("tj_trmap")).Click();//定位地图并点击
            driver.FindElement(By.XPath("//input[@id='sole-input']")).SendKeys("广东省深圳市"); //输入值
            driver.FindElement(By.Id("search-button")).Click(); //点击查询

            //获取页面源码
            Console.WriteLine(driver.PageSource);

            //关掉driver
            //driver.Quit();

            Console.WriteLine("Crawler BaseProject!");
        }
    }
}
